package com.chikitbd.common;

import org.apache.commons.net.util.Base64;

import java.io.*;

public class Util {

    public static void main(String[] args){

        String s = Util.base64decod("vmess://eyJhZGQiOiJ2MnVzMDEuaXN4YS50b3AiLCJob3N0IjoiIiwiaWQiOiJjZGQwNTQ4Mi03MWRjLTQxNjgtOWQ5ZS1jNzVmMmY0N2NkOWEiLCJuZXQiOiJ3cyIsInBhdGgiOiJcL3JheSIsInBvcnQiOiI0NDMiLCJwcyI6ImlzeC55dC0wMSIsInRscyI6InRscyIsInYiOjIsImFpZCI6NCwidHlwZSI6Im5vbmUifQo=");
        System.out.println(s);
    }


    public static String base64decod (String text){
        if(text.startsWith("vmess://")){
            text = text.substring(8);
        }
        byte[] bytes = Base64.decodeBase64(text);
        String decodeText = new String(bytes);
        return  decodeText;
    }

    public static  boolean writeFile(String writeText, String filePath) {
        boolean flag = false;
        try (PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8")));
             BufferedWriter bw = new BufferedWriter(pw);
        ) {
            bw.write(writeText); // 回写配置文件
            bw.flush();
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }


    public static String readFile(String filePath) throws IOException {
        File file = new File(filePath);
        StringBuilder fileContent = new StringBuilder();

        try (
             // 修改为BufferedReader，读取UTF-8
             //FileReader fileReader = new FileReader(file);
             //BufferedReader br = new BufferedReader(fileReader);
             InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
             BufferedReader read = new BufferedReader(isr);
        ) {
            String strTemp = "";
            while ((strTemp = read.readLine()) != null) {
                fileContent.append(strTemp);
            }
//            System.out.println(fileContent);
        }
        return fileContent.toString();
    }

    /**
     * @param serverInfos
     * @return String[]
     * @throws
     * @Title: getServerElement
     * @Description: 从配置文件中获得Element的ID
     * @Author: LanceBD
     * @Date: 2018年9月12日
     */
    public static String[] getServerElement(String serverInfos) {
        String[] strs = serverInfos.split("#");
        return strs;
    }


    public static void printfSuccess(String text){
        System.out.println(text + "-> 爬虫完成，写入配置文件完成。");
    }

}
